//======================输入框数据=============================================			
var iptList = [{
	"name": "名称 ",
	"_id": "name"
}, {
	"name": "类型",
	"_id": "types"
}, {
	"name": "上映 ",
	"_id": "year"
}, {
	"name": "地区 ",
	"_id": "city"
}, {
	"name": "导演",
	"_id": "director"
}, {
	"name": "主演",
	"_id": "star"
}, {
	"name": "海报",
	"_id": "pic"
}, {
	"name": "电影",
	"_id": "movie"
}]
//====================== 登录页组件 ===============================================			
var login = {
	template: "#login",
	data: function() {
		return {
			user: '',
			pwd: ''
		}
	},
	beforeRouteLeave: function(to, fm, next) {
		var main = {
			userName: this.user,
			passWord: this.pwd
		}
		this.$route.params.user = main.userName; //获取到登陆者信息
		$.post("/login", main, function(data) {
			if(data.info == 1) {
				$(".modal-backdrop").css({
					"display": "none"
				})
				next()
			}
		}, "json")
	}
}
//====================== 后台录入页组件 ===============================================
var addData = {
	template: "#addData",
	data: function() {
		return {
			data: iptList,
			admin: "jack"
		}
	},
	beforeRouteEnter: function(to, fm, next) {
		if(fm.params.user !== "admin" &&　!sessionStorage.opened) {
			return next({
				path: '/'
			})
		}
		next(function(vm) {
			vm.admin = to.query.admin;
			sessionStorage.admin = to.query.admin;
		})
	},
	mounted: function() {
		sessionStorage.setItem('opened', 'true');
		this.admin = sessionStorage.admin
	},
	beforeRouteLeave: function(to, fm, next) {
		this.$route.params.user = this.admin
		next()
	},
	methods: {
		demo: function(opt) {
			var item = "#" + opt
			test(item)
		},
		btnclick: function() {
			btnAjax();
		}
	}
}
//====================== 电影列表页页组件 ============================================
var AVlist = {
	template: "#AVlist",
	data: function() {
		return {
			admin: "jack",
			av: "",
			del: ""
		}
	},
	methods: {
		revise: function(opt) {
			console.log(opt)
		},
		delBtn: function(opt) {
			this.del = opt;
		},
		delAjax: function() {
			var delList = $(".delList");
			delList[this.del].style.display = "none";
			var delData = this.del;
			$.post("/del", {
				flag: delData
			}, function(data) {
				console.log(data)
			}, "json")

		}
	},
	beforeRouteEnter: function(to, fm, next) {
		if(fm.params.user !== "admin" && !sessionStorage.opened) {
			return next({
				path: '/'
			})
		}
		$.post("/list", {}, function(data) {
			var av = data;
			//调整时间格式
			av.forEach(function(item, index) {
				item.createTime = item.createTime.toString();
				var _index;
				for(var i = 0; i < item.createTime.length; i++) {
					if(item.createTime[i] == "G") {
						_index = i
					}
				}
				var _time = '';
				for(var i = 0; i < _index; i++) {
					_time += item.createTime[i]
				}
				item.createTime = _time
			})
			next(function(vm) {
				vm.admin = to.query.admin;
				sessionStorage.admin = to.query.admin;
				console.log(sessionStorage.admin)
				vm.av = av
			})
		}, "json")
	},
	mounted: function() {
		sessionStorage.setItem('opened', 'true');
		this.admin = sessionStorage.admin; 
		console.log(this.admin)
		console.log(this.av)
	},
	beforeRouteLeave: function(to, fm, next) {
		console.log(fm.query.admin)
		console.log(this.admin)
		this.$route.params.user = fm.query.admin;
		next()
	},
}

//=======================声明路由组件=====================================			
var router = new VueRouter({
	routes: [{
		path: "/",
		component: login
	}, {
		path: "/addData",
		component: addData
	}, {
		path: "/list",
		component: AVlist
	}, ]
})
//=================================声明Vue实例===========================================
new Vue({
	el: "#app",
	router: router,
})
//====================== 输入框验证方法封装========================================================
function test(el) {
	$(el).focus(function() {
		if($(this).next().length !== 0) {
			$(this).next().remove()
		}
	})
	$(el).blur(function() {
		var b = document.createElement('b');
		$(this).after(b)
		if($(this).val().trim() == "") {
			b.className = "lose";
			b.innerHTML = "不能为空";
			$(b).css({
				"display": "block",
				"color": "red",
				"margin": "5px"
			})
		} else {
			b.className = "success";
		}
	})
}
//================================提交按键方法封装========================================			
function btnAjax() {
	var len = $(".success").length;
	var iptlen = iptList.length;
	//检验有没有空输入框
	if(len == iptlen) {
		//					var data = $("#_form").serializeArray();表单序列化
		//获取录入数据
		var upData = {
			name: $("#name").val(),
			types: $("#types").val(),
			year: $("#year").val(),
			director: $("#director").val(),
			star: $("#star").val(),
			pic: $("#pic").val(),
			movie: $("#movie").val(),
			city: $("#city").val(),
			content: $("#content").val(),
			createTime: new Date(),
			flag: $("#flag").val(),
			admin: $("#admin").val()
		}

		$.post("/info", upData, function(data) {
			$("#myModalLabel").text(data.info);
			var upData = {
				name: $("#name").val(""),
				types: $("#types").val(""),
				year: $("#year").val(""),
				director: $("#director").val(""),
				star: $("#star").val(""),
				pic: $("#pic").val(""),
				movie: $("#movie").val(""),
				city: $("#city").val(""),
				content: $("#content").val(""),
			}
			$(".success").remove()
		}, "json")
	} else {
		$("#myModalLabel").text("输入内容不完整，请核对后提交");
	}

}